package com.example.xgakj.mvp_demo.ui.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.xgakj.mvp_demo.App;
import com.example.xgakj.mvp_demo.MainActivity;
import com.example.xgakj.mvp_demo.entity.PostInfo;
import com.example.xgakj.mvp_demo.mvp.BasePresenterImpl;
import com.example.xgakj.mvp_demo.mvp.module.LoginInteraction;
import com.example.xgakj.mvp_demo.ui.activities.SplashActivity;
import com.example.xgakj.mvp_demo.ui.activities.login.LoginActivity;
import com.example.xgakj.mvp_demo.ui.contract.SplashContract;
import com.example.xgakj.mvp_demo.utils.Constant;
import com.example.xgakj.mvp_demo.utils.SharedPreferencesUtil;
import com.example.xgakj.mvp_demo.utils.ToastUtil;
import com.socks.library.KLog;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by Administrator on 2017-07-18.
 */

public class SplashPresenter extends BasePresenterImpl<SplashContract.View> implements SplashContract.Presenter ,LoginInteraction.NetCallback{
    public SplashPresenter(Context context) {
        this.loginInteraction = new LoginInteraction(this);
        this.context=context;
    }
    private Context context;
    private LoginInteraction loginInteraction;
    @Override
    public void checkLogin() {
        String user = SharedPreferencesUtil.getString(App.getAppContext(), Constant.USER_NAME, "");
        String password = SharedPreferencesUtil.getString(App.getAppContext(), Constant.PASS_WORD, "");
        if (user!=""&&password!=""){
            loginInteraction.login(user,password);
        }else {
            Intent intent=new Intent(context, LoginActivity.class);
            context.startActivity(intent);
            ((SplashActivity)context).finish();
        }
    }

    @Override
    public void countDown(final int seconds) {
        Observable.interval(0,1, TimeUnit.SECONDS).take(seconds+1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return seconds-aLong;

                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    //在主线程中执行，在倒计时开始执行之前执行，可以用来初始化Progressbar
                    public void accept(Disposable disposable) throws Exception {
                        Log.i("shurun",Thread.currentThread().getName()+"doOnSubscribe");
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long value) {
                KLog.d(value+"***************************");
                if (value==0){
                    checkLogin();
                }

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                ToastUtil.showToast("jieshu");
            }
        });



    }

    @Override
    public void onSuccess(PostInfo value) {
        Intent intent=new Intent(context, MainActivity.class);
        context.startActivity(intent);
        ((SplashActivity)context).finish();
    }

    @Override
    public void onError(Throwable e) {
        Intent intent=new Intent(context, LoginActivity.class);
        context.startActivity(intent);
        ((SplashActivity)context).finish();
    }

    @Override
    public void onSubscribe(Disposable d) {

    }


}
